BluetoothPeripheralManager PRO
The BluetoothPeripheralManager API enables your device to act as a Bluetooth Low Energy (BLE) peripheral. It allows you to:
- Advertise device name and service UUIDs
- Add or remove services with characteristics
- Handle read and write requests from central devices
- Notify subscribed centrals of characteristic value changes
- Manage connection parameters such as connection latency
This API is ideal for building custom sensor devices, BLE peripheral simulators, controllers, and similar use cases.
Core Status Property
isAdvertising: Promise<boolean>
Returns whether the device is currently advertising as a peripheral.
Advertising Control
startAdvertising(advertisementData: { localName?: string; serviceUUIDs?: string[] }): Promise<void>
Begins BLE advertising with optional device name and service UUIDs.
stopAdvertising(): Promise<void>
Stops ongoing BLE advertising.
Service Management
addService(service): Promise<void>
Adds a service and its characteristics to the peripheral.
Parameters:
Example:
removeService(serviceUUID: string): Promise<void>
Removes a previously added service by its UUID.
removeAllServices(): Promise<void>
Removes all services added by the current script.
Event Handlers for Central Interaction
onRestoreState: ((state) => void) | null
Called when the system restores your script due to a background BLE session. Useful for restoring service and advertising state.
onReadyToUpdateSubscribers: (() => void) | null
Called when the system's transmission queue is cleared and ready to send notifications again after a previous failure due to queue congestion.
onReadCharacteristicValue: (characteristicId, offset, central) => Promise<{result, value}>
Invoked when a remote central requests to read a characteristic.
If not implemented, the system returns readNotPermitted.
Signature:
onWriteCharacteristicValue: (characteristicId, offset, value, central) => Promise<BluetoothATTResponseCode>
Invoked when a remote central attempts to write to a characteristic.
If not implemented, the system returns writeNotPermitted.
Signature:
onSubscribe: (characteristicId, central) => void
Called when a central subscribes to a characteristic that supports notifications or indications.
onUnsubscribe: (characteristicId, central) => void
Called when a central unsubscribes from a characteristic.
Notifications and Subscriptions
getSubscribers(characteristicId: string): Promise<Central[]>
Returns a list of central devices currently subscribed to a given characteristic.
Each item:
updateValue(characteristicId: string, value: Data, options?): Promise<boolean>
Sends a notification or indication to all subscribed centrals (or a specified subset) with the updated characteristic value.
Returns:
true: Successfully sentfalse: Queue is full — wait foronReadyToUpdateSubscribersbefore retrying
Connection Parameters
setDesiredConnectionLatency(centralId: string, latency): Promise<void>
Sets the preferred connection latency for a specific central device.
"low"– Faster interaction, higher power usage"medium"– Balanced"high"– Lower power usage, less frequent interaction
BluetoothATTResponseCode Enumeration
Defines response codes for read/write operations:
